﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public const int s = 20; 
        public const int n=265;
        public int Number1 = 0;
        public Cell[] CellArray = new Cell[n];
        public Point GetLocalMousePos()
        {
            Point screenPos = Form1.MousePosition;
            return this.PointToClient(screenPos);
        }
        public enum Stick { T, L, All, Empty }
        public class Cell
        {
            public Cell(Point P) { CellXY = P; CS = CellState.Empty; CellStick = Stick.Empty; }
            public Cell() { CellXY.X = 0; CellXY.Y = 0; CS = CellState.Empty; CellStick = Stick.Empty; }

            public enum CellState { X, O, Empty };

            public void SetStick(Stick C, ref System.Drawing.Pen myPen, ref System.Drawing.Graphics formGraphics)
            { 
                if (CellStick == Stick.Empty){ CellStick = C;
                switch (C)
                {
                    case Stick.L:
                    formGraphics.DrawLine(myPen, CellXYToFormXY(), new Point(CellXYToFormXY().X, CellXYToFormXY().Y + s));
                    break;
                    case Stick.T:
                    formGraphics.DrawLine(myPen, CellXYToFormXY(), new Point(CellXYToFormXY().X+s, CellXYToFormXY().Y));
                    break;
                }
                };
                if ((CellStick == Stick.L) && (C == Stick.T))
                {
                    CellStick = Stick.All;
                    formGraphics.DrawLine(myPen, CellXYToFormXY(), new Point(CellXYToFormXY().X + s, CellXYToFormXY().Y));
                };
                if ((CellStick == Stick.T) && (C == Stick.L))
                {
                    CellStick = Stick.All;
                    formGraphics.DrawLine(myPen, CellXYToFormXY(), new Point(CellXYToFormXY().X, CellXYToFormXY().Y + s));
                };
            }
            public Stick GetStick() {return CellStick;}

            public void ChangeCellState(bool B, ref System.Drawing.Pen myPen, ref System.Drawing.Graphics formGraphics) 
            {
                if (B)
                {
                    CS = CellState.X;
                    formGraphics.DrawLine(myPen, new Point(CellXYToFormXY().X + 4, CellXYToFormXY().Y + 4), new Point(CellXYToFormXY().X + s - 4, CellXYToFormXY().Y + s - 4));
                    formGraphics.DrawLine(myPen, new Point(CellXYToFormXY().X - 4 + s, CellXYToFormXY().Y + 4), new Point(CellXYToFormXY().X + 4, CellXYToFormXY().Y + s - 4));
                } 
                else CS = CellState.O; 
            }
            public CellState GetCellState() { return CS; }

            public Point CellXYToFormXY() 
                {
                    Point P = new Point(CellXY.X * 20, CellXY.Y * 20);
                    return P;
                }
            public Point GetCellXY() { return CellXY; }
            protected Point CellXY;
            protected Stick CellStick;
            protected CellState CS;
            

        }

        public int SearchCell(Point P)
        {
            int i=0;
            for (i=0; i<265; i++)
            {
                if(P == CellArray[i].GetCellXY()) break;
            };

            return i;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FormBorderStyle = FormBorderStyle.Fixed3D;

            Point P = new Point(0, 0);
            //for (int i = 0; i < 121; i++) { P.X = 1; P.Y; CellArray[i] = new Cell(P.X); }
            int h = 0;
            for (int i = 0; i < 11; i++)
                for (int j = 1; j < 2 + 2 * i; j++)
                { P.X = 12 + j - i - 1; P.Y = i + 1; CellArray[h] = new Cell(P); h++; }
            //
            for (int i = 0; i < 23; i++) { P.X = 1 + i; P.Y = 12; CellArray[h] = new Cell(P); h++; }
            int g = 12;
            for (int i = 11; i > 0; i--)
            {
                g++;
                for (int j = 1; j < 2 + 2 * i; j++)
                { P.X = 12 + j - i - 1; P.Y = g; CellArray[h] = new Cell(P); h++; }
            }
   
            //Cell ^[] Pointer;
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {

        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {/*
            int x, y, s = 20;
            x = (int)Math.Floor((double)GetLocalMousePos().X / s);
            y = (int)Math.Floor((double)GetLocalMousePos().Y / s);
            if ((x > (2 + 10 - y)) && (y > (2 + 10 - x)) && (x < (2 + 10 + y)) && (y < (2 + 10 + x)) && (x < (2 * 3 + 10 * 3 - y)) && (y < (2 * 3 + 10 * 3 - x)))
            {
                System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
                System.Drawing.Graphics formGraphics = this.CreateGraphics();
                formGraphics.FillRectangle(myBrush, new Rectangle(x * s, y * s, s, s));
                myBrush.Dispose();
                formGraphics.Dispose();
            }*/
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            Point P = new Point((int)Math.Floor((double)GetLocalMousePos().X / s), (int)Math.Floor((double)GetLocalMousePos().Y / s));
            if (e.Button == MouseButtons.Left) Number1 = SearchCell(P);
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            Point P = new Point((int)Math.Floor((double)GetLocalMousePos().X / s), (int)Math.Floor((double)GetLocalMousePos().Y / s));
            System.Drawing.Pen myPen = new System.Drawing.Pen(Color.Blue, 1);
            System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
            System.Drawing.Graphics formGraphics = this.CreateGraphics();
            int Number2 = SearchCell(new Point(CellArray[Number1].GetCellXY().X, CellArray[Number1].GetCellXY().Y + 1));
            if (CellArray[Number1].GetCellXY().X > GetLocalMousePos().X / s) CellArray[Number1].SetStick(Stick.L, ref myPen, ref formGraphics);
            if (CellArray[Number1].GetCellXY().X < GetLocalMousePos().X / s) CellArray[Number1 + 1].SetStick(Stick.L, ref myPen, ref formGraphics);
            if (CellArray[Number1].GetCellXY().Y > GetLocalMousePos().Y / s) CellArray[Number1].SetStick(Stick.T, ref myPen, ref formGraphics);
            if (CellArray[Number1].GetCellXY().Y < GetLocalMousePos().Y / s) CellArray[Number2].SetStick(Stick.T, ref myPen, ref formGraphics);

            if ((CellArray[Number1].GetStick() == Stick.All)
                && (CellArray[Number1 + 1].GetStick() == Stick.L || CellArray[Number1 + 1].GetStick() == Stick.All) &&
                 (CellArray[Number2].GetStick() == Stick.T || CellArray[Number2].GetStick() == Stick.All)) CellArray[Number1].ChangeCellState(true, ref  myPen, ref  formGraphics);

            if ((CellArray[Number1].GetStick() == Stick.All)
                && (CellArray[Number1 + 1].GetStick() == Stick.L || CellArray[Number1 + 1].GetStick() == Stick.All) &&
                 (CellArray[Number2].GetStick() == Stick.T || CellArray[Number2].GetStick() == Stick.All)) CellArray[Number1].ChangeCellState(true, ref  myPen, ref  formGraphics);

            myBrush.Dispose();
            myPen.Dispose();
            formGraphics.Dispose();
        }
    }
}
